define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wdsxh/single/order/index' + location.search,
                    // add_url: 'mall/order/add',
                    // edit_url: 'mall/order/edit',
                    // del_url: 'mall/order/del',
                    // multi_url: 'mall/order/multi',
                    // import_url: 'mall/order/import',
                    table: 'mall_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'parent_id', title: __('Parent_id')},
                        // {field: 'buy_now', title: __('Buy_now'), searchList: {"1":__('Buy_now 1'),"2":__('Buy_now 2')}, formatter: Table.api.formatter.normal},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        // {field: 'trade_no', title: __('Trade_no'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        {
                            field: 'goods', title: '商品信息', formatter: function (value, row, index) {
                                var return_result = '';
                                $.each(row.goods,function(i,item){
                                    return_result = return_result + "<img width='40px' height='40px' src = "+item.goods_image+">"+ item.goods_name + "￥" + item.goods_price +"x"+item.goods_num+item.unit+"</br></br>";
                                });
                                return return_result;
                            }
                        },
                        {field: 'real_name', title: __('Real_name'), operate: 'LIKE'},
                        {field: 'user_phone', title: __('User_phone'), operate: 'LIKE'},
                        {field: 'user_address', title: __('User_address'), operate: 'LIKE'},
                        // {field: 'cart_ids', title: __('Cart_ids'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        // {field: 'split_fail_reason', title: __('Split_fail_reason'), operate: 'LIKE'},
                        // {field: 'refund_status', title: __('Refund_status'), searchList: {"0":__('Refund_status 0'),"1":__('Refund_status 1'),"2":__('Refund_status 2'),"3":__('Refund_status 3'),"4":__('Refund_status 4')}, formatter: Table.api.formatter.status},
                        // {field: 'total_num', title: __('Total_num')},
                        // {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        // {field: 'pay_postage', title: __('Pay_postage'), operate:'BETWEEN'},
                        {field: 'paid', title: __('Paid'), searchList: {"0":__('Paid 0'),"1":__('Paid 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'refund_price', title: __('Refund_price'), operate:'BETWEEN'},
                        // {field: 'refund_time', title: __('Refund_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'refund_reason', title: __('Refund_reason'), operate: 'LIKE'},
                        // {field: 'refund_express_no', title: __('Refund_express_no'), operate: 'LIKE'},
                        // {field: 'complete_time', title: __('Complete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'cancel_time', title: __('Cancel_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    text:'发货',
                                    name: 'logistics',
                                    title: '发货',
                                    classname: 'btn btn-xs btn-primary btn-dialog bg-aqua',
                                    icon: 'fa',
                                    url: 'wdsxh/mall/logistics/logistics',
                                    extend:'data-area=["80%","80%"]',
                                    visible: function (row) {
                                        if(row['status'] == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    text:'订单详情',
                                    name: 'goods_details',
                                    title: '订单详情',
                                    classname: 'btn btn-xs btn-primary btn-dialog bg-aqua',
                                    icon: 'fa',
                                    url: 'wdsxh/mall/order/goods_details',
                                    extend:'data-area=["95%","95%"]',
                                    visible: function (row) {
                                        if(row['status'] == 2 && row['admin_show'] == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                            ],}

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // recyclebin: function () {
        //     // 初始化表格参数配置
        //     Table.api.init({
        //         extend: {
        //             'dragsort_url': ''
        //         }
        //     });
        //
        //     var table = $("#table");
        //
        //     // 初始化表格
        //     table.bootstrapTable({
        //         url: 'mall/order/recyclebin' + location.search,
        //         pk: 'id',
        //         sortName: 'id',
        //         columns: [
        //             [
        //                 {checkbox: true},
        //                 {field: 'id', title: __('Id')},
        //                 {
        //                     field: 'deletetime',
        //                     title: __('Deletetime'),
        //                     operate: 'RANGE',
        //                     addclass: 'datetimerange',
        //                     formatter: Table.api.formatter.datetime
        //                 },
        //                 {
        //                     field: 'operate',
        //                     width: '130px',
        //                     title: __('Operate'),
        //                     table: table,
        //                     events: Table.api.events.operate,
        //                     buttons: [
        //                         {
        //                             name: 'Restore',
        //                             text: __('Restore'),
        //                             classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
        //                             icon: 'fa fa-rotate-left',
        //                             url: 'mall/order/restore',
        //                             refresh: true
        //                         },
        //                         {
        //                             name: 'Destroy',
        //                             text: __('Destroy'),
        //                             classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
        //                             icon: 'fa fa-times',
        //                             url: 'mall/order/destroy',
        //                             refresh: true
        //                         }
        //                     ],
        //                     formatter: Table.api.formatter.operate
        //                 }
        //             ]
        //         ]
        //     });
        //
        //     // 为表格绑定事件
        //     Table.api.bindevent(table);
        // },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});